﻿using System.Linq;
using System.Xml.Linq;

namespace 常用扩展方法
{
    internal class Program
    {
        static void Main(string[] args)
        {
            List<Employee> list = new List<Employee>();
            list.Add(new Employee { Id = 1, Name = "jerry", Age = 28, Gender = true, Salary = 5000 });
            list.Add(new Employee { Id = 2, Name = "jim", Age = 33, Gender = true, Salary = 3000 });
            list.Add(new Employee { Id = 3, Name = "lily", Age = 35, Gender = false, Salary = 9000 });
            list.Add(new Employee { Id = 4, Name = "lucy", Age = 16, Gender = false, Salary = 2000 });
            list.Add(new Employee { Id = 5, Name = "kimi", Age = 25, Gender = true, Salary = 1000 });
            list.Add(new Employee { Id = 6, Name = "nancy", Age = 35, Gender = false, Salary = 8000 });
            list.Add(new Employee { Id = 7, Name = "zack", Age = 35, Gender = true, Salary = 8500 });
            list.Add(new Employee { Id = 8, Name = "jack", Age = 33, Gender = true, Salary = 8000 });

            IEnumerable<Employee> list1 = list.Where(e => e.Age > 30);
            foreach (Employee emp in list1)
            {
                Console.WriteLine(emp);
            }
            Console.WriteLine(list.Count());
            Console.WriteLine(list.Count(e => e.Age > 30 && e.Salary > 8000));

            IEnumerable<Employee> list2 = list.Where(e => e.Id == 8);
            Employee e = list2.Single();

            Employee e1 = list.Where(e => e.Id == 7).Single();

            Employee e2 = list.Single(e => e.Id == 6);
            Console.WriteLine(e);
            Console.WriteLine(e1);
            Console.WriteLine(e2);

            //IEnumerable<Employee> list3 = list.OrderBy(e => e.Age);
            IEnumerable<Employee> list3 = list.OrderByDescending(e => e.Age);
            foreach (Employee emp in list3)
            {
                Console.WriteLine(emp);
            }

            IEnumerable<Employee> list4 = list.Skip(2).Take(4);
            //IEnumerable<Employee> list4 = list.Skip(2);
            //IEnumerable<Employee> list4 = list.Take(4);
            //IEnumerable<Employee> list4 = list.Where(e => e.Age > 30).OrderBy(e => e.Age).Skip(2).Take(4);
            foreach (Employee emp in list4)
            {
                Console.WriteLine(emp);
            }
            //分组
            Console.WriteLine("=================分组=================");
            IEnumerable<IGrouping<int, Employee>> list5 = list.GroupBy(e => e.Age);
            foreach (IGrouping<int, Employee> emp in list5)
            {
                Console.WriteLine(emp.Key);
                foreach (Employee a in emp)
                {
                    Console.WriteLine(a);
                }
                Console.WriteLine("===================================");
            }
            //投影
            Console.WriteLine("=================投影=================");
            IEnumerable<int> list6 = list.Select(e => e.Age);
            foreach (int emp in list6)
            {
                Console.WriteLine(emp);
            }
            IEnumerable<string> list7 = list.Select(e => e.Name);
            //IEnumerable<string> list7 = list.Select(e => e.Name+'-'+e.Id);
            foreach (string emp in list7)
            {
                Console.WriteLine(emp);
            }
            IEnumerable<Dog> list8 = list.Select(e => new Dog { NickName = e.Name, Age = e.Age });
            foreach (Dog emp in list8)
            {
                Console.WriteLine($"{emp.NickName}-{emp.Age}");
            }
            //匿名类型
            Console.WriteLine("=================匿名类型=================");
            var p = new { Name = "tom", Id = 1 };
            Console.WriteLine($"{p.Name}-{p.Id}");
            //投影与匿名类型：
            Console.WriteLine("=================投影与匿名类型=================");
            var items = list.Select(e => new { e.Name, e.Age, XingBie = e.Gender ? "男" : "女" });
            var items1 = list.GroupBy(e => e.Gender).Select(g => new { Gender = g.Key, Count = g.Count(), AvgSalary = g.Average(e => e.Salary), MinAge = g.Min(e => e.Age) });

            IEnumerable<Employee> list9 = list.Where(e => e.Age > 30);
            List<Employee> list10 = list9.ToList();
            Employee[] list11 = list9.ToArray();
            //查询语法
            Console.WriteLine("=================查询语法=================");
            var list12 = list.Where(e => e.Salary > 3000).OrderBy(d => d.Age).Select(s => new { s.Name, s.Age, XingBie = s.Gender ? "男" : "女" });
            var list13 = from e11 in list where e11.Salary > 3000 orderby e11.Age select new { e11.Name, e11.Age, Gender = e11.Gender ? "男" : "女" };
            foreach (var s in list12)
            {
                Console.WriteLine($"{s.Name}-{s.Age}-{s.XingBie}");
            }
            foreach (var s in list13)
            {
                Console.WriteLine($"{s.Name}-{s.Age}-{s.Gender}");
            }
        }
    }
}